home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / plnk081.zip / pilot-link.0.8.1 / libsock / utils.c < prev   
C/C++ Source or Header  |  1997-08-05  |  6KB  |  260 lines

  1. /* utils.c:  misc. stuff for dealing with packets.
  2.  *
  3.  * Portions Copyright (c) 1996, D. Jeff Dionne.
  4.  * Portions Copyright (c) 1996, Kenneth Albanowski
  5.  *
  6.  * This is free software, licensed under the GNU Public License V2.
  7.  * See the file COPYING for details.
  8.  */
  9.  
  10. #include <stdio.h>
  11. #include <ctype.h>
  12. #include <math.h>
  13.  
  14. #ifdef NeXT
  15. # include <stdlib.h>
  16. # include <string.h>
  17. # include <assert.h>
  18. #endif
  19.  
  20. #ifndef HAVE_INET_ATON
  21. # include <sys/param.h>
  22. #ifdef __EMX__
  23. # include <sys/types.h>
  24. #endif
  25. # include <netinet/in.h>
  26. # include <arpa/inet.h>
  27. # include <ctype.h>
  28. #endif
  29.  
  30. #include "pi-source.h"
  31. #include "pi-socket.h"
  32.  
  33. /* this routine ruthlessly stolen verbatim from Brian J. Swetland */
  34.  
  35. int crc16(unsigned char *ptr, int count)
  36. {
  37.   int crc, i;
  38.  
  39.   crc = 0;
  40.   while(--count >= 0) {
  41.     crc = crc ^ (int)*ptr++ << 8;
  42.     for(i = 0; i < 8; ++i)
  43.       if(crc & 0x8000)
  44.     crc = crc << 1 ^ 0x1021;
  45.       else
  46.     crc = crc << 1;
  47.   }
  48.   return (crc & 0xFFFF);
  49. }
  50.  
  51. #ifndef HAVE_STRDUP
  52. char * strdup(const char *string)
  53. {
  54.     size_t length;
  55.     char *result;
  56.  
  57.     assert(string != NULL);
  58.  
  59.     length = strlen(string) + 1;
  60.     result = malloc(length);
  61.  
  62.     if (result == NULL)
  63.     return NULL;
  64.  
  65.     memcpy(result, string, length);
  66.  
  67.     return result;
  68. }
  69. #endif
  70.  
  71. #ifndef HAVE_INET_ATON
  72. int
  73. inet_aton(cp, addr)
  74.         register const char *cp;
  75.         struct in_addr *addr;
  76. {
  77.         register u_long val;
  78.         register int base, n;
  79.         register char c;
  80.         u_int parts[4];
  81.         register u_int *pp = parts;
  82.  
  83.         for (;;) {
  84.                 /*
  85.                  * Collect number up to ``.''.
  86.                  * Values are specified as for C:
  87.                  * 0x=hex, 0=octal, other=decimal.
  88.                  */
  89.                 val = 0; base = 10;
  90.                 if (*cp == '0') {
  91.                         if (*++cp == 'x' || *cp == 'X')
  92.                                 base = 16, cp++;
  93.                         else
  94.                                 base = 8;
  95.                 }
  96.                 while ((c = *cp) != '\0') {
  97.                         if (isascii(c) && isdigit(c)) {
  98.                                 val = (val * base) + (c - '0');
  99.                                 cp++;
  100.                                 continue;
  101.                         }
  102.                         if (base == 16 && isascii(c) && isxdigit(c)) {
  103.                                 val = (val << 4) +
  104.                                         (c + 10 - (islower(c) ? 'a' : 'A'));
  105.                                 cp++;
  106.                                 continue;
  107.                         }
  108.                         break;
  109.                 }
  110.                 if (*cp == '.') {
  111.                         /*
  112.                          * Internet format:
  113.                          *      a.b.c.d
  114.                          *      a.b.c   (with c treated as 16-bits)
  115.                          *      a.b     (with b treated as 24 bits)
  116.                          */
  117.                         if (pp >= parts + 3 || val > 0xff)
  118.                                 return (0);
  119.                         *pp++ = val, cp++;
  120.                 } else
  121.                         break;
  122.         }
  123.         /*
  124.          * Check for trailing characters.
  125.          */
  126.         if (*cp && (!isascii(*cp) || !isspace(*cp)))
  127.                 return (0);
  128.         /*
  129.          * Concoct the address according to
  130.          * the number of parts specified.
  131.          */
  132.         n = pp - parts + 1;
  133.         switch (n) {
  134.  
  135.         case 1:                         /* a -- 32 bits */
  136.                 break;
  137.  
  138.         case 2:                         /* a.b -- 8.24 bits */
  139.                 if (val > 0xffffff)
  140.                         return (0);
  141.                 val |= parts[0] << 24;
  142.                 break;
  143.  
  144.         case 3:                         /* a.b.c -- 8.8.16 bits */
  145.                 if (val > 0xffff)
  146.                         return (0);
  147.                 val |= (parts[0] << 24) | (parts[1] << 16);
  148.                 break;
  149.  
  150.         case 4:                         /* a.b.c.d -- 8.8.8.8 bits */
  151.                 if (val > 0xff)
  152.                         return (0);
  153.                 val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
  154.                 break;
  155.         }
  156.         if (addr)
  157.                 addr->s_addr = htonl(val);
  158.         return (1);
  159. }
  160. #endif
  161.  
  162. char * printlong (unsigned long val)
  163. {
  164.   static char buf[5];
  165.   set_long(buf, val);
  166.   buf[4] = 0;
  167.   return buf;
  168. }
  169.  
  170. unsigned long makelong (char * c)
  171. {
  172.   char c2[4];
  173.   int l = strlen(c);
  174.   if (l>=4)
  175.     return get_long(c);
  176.   memset(c2, ' ', 4);
  177.   memcpy(c2, c, l);
  178.   return get_long(c2);
  179. }
  180.  
  181. void dumpline (const unsigned char *buf, int len, int addr)
  182. {
  183.   int i;
  184.  
  185.   fprintf (stderr,"%.4x  ",addr);
  186.  
  187.   for (i=0;i<16;i++) {
  188.  
  189.     if (i<len) fprintf (stderr,"%.2x ",0xff & (unsigned int)buf[i]);
  190.     else fprintf (stderr,"   ");
  191.   }
  192.  
  193.   fprintf (stderr,"  ");
  194.  
  195.   for (i=0;i<len;i++) {
  196.     if (isprint(buf[i]) && (buf[i]>=32) && (buf[i]<=126)) fprintf (stderr,"%c",buf[i]);
  197.     else fprintf(stderr,".");
  198.   }
  199.   fprintf(stderr,"\n");
  200. }
  201.  
  202. void dumpdata (const unsigned char * buf, int len) {
  203.   int i;
  204.   for(i=0;i<len;i+=16) {
  205.     dumpline(buf+i, ((len-i)>16) ? 16 : len-i, i);
  206.   }
  207. }
  208.  
  209. double get_float(void * buffer) {
  210.     unsigned char * buf = buffer;
  211.  
  212.     /* Load values */
  213.     unsigned long frac = get_long(buf);
  214.     int exp = get_sshort(buf+4);
  215.     int sign = get_byte(buf+6);
  216.     
  217.     return ldexp(sign ? (double)frac : -(double)frac, exp);
  218. }
  219.  
  220. void set_float(void * buffer, double value) {
  221.     unsigned char * buf = buffer;
  222.     
  223.     unsigned long frac;
  224.     int exp, sign;
  225.     
  226.     /* Take absolute */
  227.     if (value < 0) {
  228.         sign=0;
  229.         value = -value;
  230.     } else
  231.         sign=0xFF;
  232.     
  233.     /* Convert mantissa to 32-bit integer, and take exponent */
  234.     frac = ldexp(frexp(value, &exp), 32);
  235.     exp -= 32;
  236.     
  237.     /* Store values in buffer */
  238.     set_long(buf, frac);
  239.     set_sshort(buf+4, exp);
  240.     set_byte(buf+6, sign);
  241.     set_byte(buf+7, 0);
  242. }
  243.  
  244. int compareTm(struct tm *a, struct tm *b)
  245. {
  246.     int d;
  247.     d = a->tm_year - b->tm_year;
  248.     if (d) return d;
  249.     d = a->tm_mon - b->tm_mon;
  250.     if (d) return d;
  251.     d = a->tm_mday - b->tm_mday;
  252.     if (d) return d;
  253.     d = a->tm_hour - b->tm_hour;
  254.     if (d) return d;
  255.     d = a->tm_min - b->tm_min;
  256.     if (d) return d;
  257.     d = a->tm_sec - b->tm_sec;
  258.     return d;
  259. }
  260.